using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Turret : MonoBehaviour
{
	public Tool tool;

	public Weapon weapon;

	public GameObject joint;

	public GameObject ammoBox;

	public Collider2D fireTrigger;

	public float overheat;

	public ParticleSystem overheatPs;

	public GameObject itemListCanvas;

	public GameObject itemListView;

	public Button itemViewPrefab;

	public float restUntil;

	public List<Zombie> targets = new List<Zombie>();

	public Scene scene;

	public bool isPlayground;

	private void Awake()
	{
	}

	private void Start()
	{
		if (!weapon)
		{
			ShowWeaponList();
		}
	}

	public void ShowWeaponList()
	{
		isPlayground = SceneManager.GetActiveScene().name == "PlaygroundGame";
		Time.timeScale = 0f;
		itemListCanvas.SetActive(value: true);
		List<string> list = DO.Campaign.availableItems;
		List<string> list2 = new List<string>();
		if (isPlayground)
		{
			list = DO.Player.reachedItems;
		}
		else
		{
			foreach (StageItem stageItem in DO.Campaign.stageItems)
			{
				if (stageItem.usedItemName != "")
				{
					list2.Add(stageItem.usedItemName);
				}
			}
		}
		foreach (KeyValuePair<string, Item> kvp in CampaignGame.itemCache)
		{
			if (!list2.Contains(kvp.Key) && list.Contains(kvp.Key) && kvp.Value.turretUsable && kvp.Value.type == ItemType.Weapon && (isPlayground || (!(kvp.Key == DO.Campaign.primaryWeapon) && !(kvp.Key == DO.Campaign.secondaryWeapon))))
			{
				Button button = Object.Instantiate(itemViewPrefab, itemListView.transform);
				button.transform.Find("Title").GetComponent<Text>().text = kvp.Value.title;
				button.onClick.AddListener(delegate
				{
					Weapon component = Object.Instantiate(kvp.Value.prefab).GetComponent<Weapon>();
					AddWeapon(component);
				});
			}
		}
	}

	public void AddWeapon(Weapon weaponPrefab)
	{
		weapon = weaponPrefab;
		weapon.transform.SetParent(ammoBox.transform, worldPositionStays: false);
		weapon.transform.localPosition = weapon.transform.position - weapon.magazineWell.transform.position;
		weapon.audioSource.volume = 0.5f;
		Time.timeScale = 1f;
		itemListCanvas.SetActive(value: false);
		tool.usedItemName = weaponPrefab.gameObject.name.Replace("(Clone)", "");
		SetWeaponLayer();
	}

	public void SetWeaponLayer()
	{
		int num = 31;
		foreach (GameObject groundCollider in Ground.groundColliders)
		{
			if (groundCollider.transform.position.y <= base.transform.position.y)
			{
				num = groundCollider.layer + Ground.groundTotal + weapon.bulletShell.colliderLayer;
				break;
			}
		}
		if (num > 31)
		{
			num = 31;
		}
		weapon.bulletShell.gameObject.layer = num;
	}

	private void FixedUpdate()
	{
		if (!tool.placed)
		{
			return;
		}
		if (overheat > 0f)
		{
			overheat -= 0.001f;
		}
		if (restUntil > Time.time)
		{
			return;
		}
		if (!fireTrigger.enabled && tool.placed)
		{
			fireTrigger.enabled = true;
			SetWeaponLayer();
		}
		if (overheat > 1f)
		{
			restUntil = Time.time + weapon.damage;
			joint.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, -45f));
			weapon.Stop();
			return;
		}
		ParticleSystem.EmissionModule emission = overheatPs.emission;
		if (overheat > 0.5f)
		{
			emission.rateOverTime = Mathf.CeilToInt(overheat * 10f);
		}
		else
		{
			emission.rateOverTime = 0f;
		}
		if ((bool)weapon && targets.Count > 0)
		{
			FaceTarget();
			if (!weapon.onFire)
			{
				weapon.OpenFire();
			}
			weapon.Fire();
			overheat += 0.002f;
		}
		else
		{
			weapon.Stop();
		}
	}

	public void FaceTarget()
	{
		if (targets.Count == 0)
		{
			return;
		}
		if (!targets[0] || !targets[0].body)
		{
			targets.RemoveAt(0);
			return;
		}
		Vector3 position = joint.transform.position;
		position.z = 0f;
		Vector3 position2 = targets[0].body.chest.transform.position;
		position.x = position2.x - position.x;
		position.y = position2.y - position.y;
		if (weapon.onFire)
		{
			float z = Mathf.Atan2(position.y, position.x) * 57.29578f + Random.Range((0f - weapon.recoil) / 5f, weapon.recoil / 5f);
			joint.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, z));
		}
	}

	private void OnTriggerEnter2D(Collider2D collision)
	{
		if (collision.tag == "Zombie" && collision.gameObject.TryGetComponent<Zombie>(out var component) && (bool)component.body)
		{
			targets.Add(component);
		}
	}

	private void OnTriggerExit2D(Collider2D collision)
	{
		if (collision.tag == "Zombie" && collision.gameObject.TryGetComponent<Zombie>(out var component) && (bool)component.body)
		{
			targets.Add(component);
		}
	}
}
